package com.hellorxjava.demo;



import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableSource;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.core.Scheduler;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Supplier;

/**
 * Observable.range(start,count) 依次开始发送从n开始的m个数 从n到 n+m-1
 * Observable.just(T t) 接收某个对象作为输入
 * Observable.defer(Supplier<? extends ObservableSource<? extends T>> supplier)  每次订阅都会创建最新的Observable对象 也就是说多次调用数据会一样
 *
 * Observable.fromIterable()
 * Observable.fromCallable()
 * Observable.fromFuture()
 * Observable.fromArray()  分多次将里面的数据发送出来
 *
 * Observable.interval()  每隔一段时间发送 0至其他 需要自己停止
 * Observable.repeat() 重复几次发送事件
 * Observable.timer() 延迟一段时间发送 0
 */
public class RXJavaDemo4 {
    public static void main(String[] args) {
        Observable.range(10,5).subscribe(new Observer<Integer>() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {
                System.out.println("onSubscribe");
            }

            @Override
            public void onNext(@NonNull Integer integer) {
                System.out.println("onNext: " + integer);
            }

            @Override
            public void onError(@NonNull Throwable e) {
                System.out.println("onError: " + e.getMessage());
            }

            @Override
            public void onComplete() {
                System.out.println("onComplete: ");
            }
        });


        getJust().subscribe(new Observer<Long>() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {
                System.out.println("Just subscribe");
            }

            @Override
            public void onNext(@NonNull Long aLong) {
                System.out.println("Just onNext" + aLong);
            }

            @Override
            public void onError(@NonNull Throwable e) {

            }

            @Override
            public void onComplete() {
                System.out.println("Just onComplete");
            }
        });

        getDefer().subscribe(new Observer<Long>() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {
                System.out.println("Defer subscribe");
            }

            @Override
            public void onNext(@NonNull Long aLong) {
                System.out.println("Defer onNext" + aLong);
            }

            @Override
            public void onError(@NonNull Throwable e) {

            }

            @Override
            public void onComplete() {
                System.out.println("Defer onComplete");
            }
        });

        getDefer().subscribe(new Observer<Long>() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {
                System.out.println("Defer subscribe");
            }

            @Override
            public void onNext(@NonNull Long aLong) {
                System.out.println("Defer onNext" + aLong);
            }

            @Override
            public void onError(@NonNull Throwable e) {

            }

            @Override
            public void onComplete() {
                System.out.println("Defer onComplete");
            }
        });

        getFromArray().subscribe(new Observer<Integer>() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {
                System.out.println("FromArray subscribe");
            }

            @Override
            public void onNext(@NonNull Integer aInteger) {
                System.out.println("FromArray onNext" + aInteger);
            }

            @Override
            public void onError(@NonNull Throwable e) {

            }

            @Override
            public void onComplete() {
                System.out.println("FromArray onComplete");
            }
        });

        getFromIterable().subscribe(new Observer<Integer>() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {
                System.out.println("FromIterable subscribe");
            }

            @Override
            public void onNext(@NonNull Integer aInteger) {
                System.out.println("FromIterable onNext" + aInteger);
            }

            @Override
            public void onError(@NonNull Throwable e) {

            }

            @Override
            public void onComplete() {
                System.out.println("FromIterable onComplete");
            }
        });

        final Observer<Long> intervalObserver = new Observer<Long>() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {
                System.out.println("interval subscribe");
            }

            @Override
            public void onNext(@NonNull Long aLong) {
                System.out.println("interval onNext " + aLong);
            }

            @Override
            public void onError(@NonNull Throwable e) {

            }

            @Override
            public void onComplete() {
                System.out.println("FromIterable onComplete");
            }
        };
        interval().subscribe(intervalObserver);

        repeatObserver().subscribe(new Observer<Integer>() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {
                System.out.println("repeat subscribe");
            }

            @Override
            public void onNext(@NonNull Integer aInteger) {
                System.out.println("repeat onNext：" + aInteger);
            }

            @Override
            public void onError(@NonNull Throwable e) {

            }

            @Override
            public void onComplete() {
                System.out.println("repeat onComplete");
            }
        });

    }

    private static Observable<Long> getJust() {
        return Observable.just(System.currentTimeMillis());
    }

    private static Observable<Long> getDefer() {
        return Observable.defer(new Supplier<ObservableSource<? extends Long>>() {
            @Override
            public ObservableSource<? extends Long> get() throws Throwable {
                return getJust();
            }
        });
    }

    private static Observable<Integer> getFromArray(){
        Integer[] arrays = {1,2,3,4,5};
        return Observable.fromArray(arrays);
    }

    private static Observable<Integer> getFromIterable(){
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            list.add(i);
        }
        return Observable.fromIterable(list);
    }

    private static Observable<Long> interval(){
        return Observable.interval(1, TimeUnit.SECONDS);
    }

    private static Observable<Integer> repeatObserver(){
        return Observable.just(1,2,3).repeat(3);
    }

    private static Observable<Long> timerObserver(){
        return Observable.timer(1, TimeUnit.SECONDS);
    }
}
